package algorithm.easy;

import java.util.Stack;

/**
 * @BelongsProject: LeetCode
 * @BelongsPackage: algorithm.easy
 * @Author: 江岸
 * @CreateTime: 2021-03-05 00:14
 * @Description: 实现队列
 */
public class MyQueue232 {
    private Stack<Integer> stack1;
    private Stack<Integer> stack2;

    /** Initialize your data structure here. */
    public MyQueue232() {
        this.stack1 = new Stack<>();
        this.stack2 = new Stack<>();
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        stack1.add(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        while (!stack1.isEmpty()){
            stack2.add(stack1.pop());
        }
        int res = stack2.pop();
        while (!stack2.isEmpty()){
            stack1.add(stack2.pop());
        }
        return res;
    }

    /** Get the front element. */
    public int peek() {
        while (!stack1.isEmpty()){
            stack2.add(stack1.pop());
        }
        int res = stack2.peek();
        while (!stack2.isEmpty()){
            stack1.add(stack2.pop());
        }
        return res;
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stack1.isEmpty();
    }
}
